package com.seio.edc.controller.warning;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.energy.Gather;
import com.nova.edc.model.energy.RealData;
import com.nova.edc.model.system.AreaPoint;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.GatherService;
import com.nova.edc.service.system.AreaPointService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.nova.frame.utils.JdbcUtils;
import com.nova.frame.utils.StringUtils;
import com.nova.module.sql.SqlBuffer;
import com.seio.edc.common.Constants;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/8 0008.
 */
@Controller
@RequestMapping(value = "/warning")
public class WarningController {

    @Autowired
    private GatherService novaGatherService;
    @Autowired
    private CustomerInfoService novaCustomerInfoService;
    @Autowired
    private AreaPointService novaAreapointServices;

    private static final int PAGE_SIZE = 10;

    @RequestMapping(value = "/warningInit")
    public ModelAndView warningInit(HttpServletRequest request,
                                    HttpSession session, Integer pageIndex, ModelMap modelMap) {
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        String date = DateUtils.getDate2SStr(DateUtils.getYesterdayDate());
        Login login = (Login) session.getAttribute("sessionLogin");
        List<CustomerInfo> customerInfos = null;
        if (login.getRoletype() == ModelConstant.ROLE_GROUP) {
            customerInfos = novaCustomerInfoService.findByGroupcode(login.getGroupcode());
        } else {
            customerInfos = novaCustomerInfoService
                    .findCustomerInfoListByRole(login.getUsername(), null, "", "");
        }
        List<String> customerGateway = novaAreapointServices.getCustomerGateway(customerInfos);
        List<Map<String, Object>> warning = novaGatherService.getWarning(page, date, ModelConstant.LIGHT_ON, customerGateway, ModelConstant.WARNING_TIMES, ModelConstant.DEVICE_LIGHTENERGY);
//        List<Map<String, Object>> list = novaAreapointServices.getAllGatewayCustomer();
//        modelMap.put("gateways", JSON.toJSONString(list));
        modelMap.put("customername", "");
        modelMap.put("companycode", "");
        modelMap.put("warning", warning);
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        modelMap.put("endDate", date);
        return new ModelAndView("warning/warningList", modelMap);
    }

    @RequestMapping(value = "/selectWaring")
    public ModelAndView checkWaring(HttpServletRequest request,
                                    HttpSession session, Integer pageIndex, ModelMap modelMap, String gatewayId,
                                    CustomerInfo customerInfo, String endDate) {
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        if (endDate == null || endDate == "") {
            endDate = DateUtils.getDate2SStr(DateUtils.getYesterdayDate());
        }
        List<String> customerGateway = new ArrayList<String>();
        if (StringUtils.isNullOrEmpty(customerInfo.getCustomername()) && !IntegerUtils.isEmptyAndNull(customerInfo.getCompanycode())) {
            Login login = (Login) session.getAttribute("sessionLogin");
            List<CustomerInfo> customerInfos = null;
            if (login.getRoletype() == ModelConstant.ROLE_GROUP) {
                customerInfos = novaCustomerInfoService.findByGroupcode(login.getGroupcode());
            } else {
                customerInfos = novaCustomerInfoService
                        .findCustomerInfoListByRole(login.getUsername(), null, "", "");
            }
            customerGateway = novaAreapointServices.getCustomerGateway(customerInfos);
        } else {
            List<CustomerInfo> customerInfoList = novaCustomerInfoService.findByCodeNameLike(customerInfo);
            List<String> gateway = novaAreapointServices.getCustomerGateway(customerInfoList);
            customerGateway.addAll(gateway);
//            customerGateway.add(gatewayId);
//            modelMap.put("gatewayId", gatewayId);
        }
        List<Map<String, Object>> warning = novaGatherService.getWarning(page, endDate, ModelConstant.LIGHT_ON, customerGateway, ModelConstant.WARNING_TIMES, ModelConstant.DEVICE_LIGHTENERGY);
//        List<Map<String, Object>> list = novaAreapointServices.getAllGatewayCustomer();
//        modelMap.put("gateways", JSON.toJSONString(list));
        modelMap.put("customername", customerInfo.getCustomername() == null ? "" : customerInfo.getCustomername());
        modelMap.put("companycode", customerInfo.getCompanycode() == null ? "" : customerInfo.getCompanycode());
        modelMap.put("warning", warning);
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        modelMap.put("gatewayId", gatewayId);
        modelMap.put("endDate", endDate);
        return new ModelAndView("warning/warningList", modelMap);
    }

    @RequestMapping(value = "/allnightInit")
    public ModelAndView allnightInit(HttpServletRequest request,
                                     HttpSession session, Integer pageIndex, ModelMap modelMap, String gatewayId,
                                     CustomerInfo customerInfo, String endDate) {
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        if (StringUtils.isNullOrEmpty(endDate)) {
            endDate = DateUtils.getDate2SStr(DateUtils.getYesterdayDate());
        }
        List<String> customerGateway = new ArrayList<String>();
        if (StringUtils.isNullOrEmpty(customerInfo.getCustomername()) && !IntegerUtils.isEmptyAndNull(customerInfo.getCompanycode())) {
            Login login = (Login) session.getAttribute("sessionLogin");
            List<CustomerInfo> customerInfos = null;
            if (login.getRoletype() == ModelConstant.ROLE_GROUP) {
                customerInfos = novaCustomerInfoService.findByGroupcode(login.getGroupcode());
            } else {
                customerInfos = novaCustomerInfoService
                        .findCustomerInfoListByRole(login.getUsername(), null, "", "");
            }
            customerGateway = novaAreapointServices.getCustomerGateway(customerInfos);
        } else {
            List<CustomerInfo> customerInfoList = novaCustomerInfoService.findByCodeNameLike(customerInfo);
            List<String> gateway = novaAreapointServices.getCustomerGateway(customerInfoList);
            customerGateway.addAll(gateway);
//            customerGateway.add(gatewayId);
//            modelMap.put("gatewayId", gatewayId);
        }
        List<Map<String, Object>> allnight = novaGatherService.getAllnight(page, endDate, ModelConstant.LIGHT_ON, customerGateway, ModelConstant.WARNING_MIN_TIME, ModelConstant.DEVICE_AIR);
        novaGatherService.getAllnightTime(ModelConstant.WARNING_MIN_TIME, ModelConstant.LIGHT_ON, endDate, allnight);
        //  List<Map<String, Object>> list = novaAreapointServices.getAllGatewayCustomer();
        //  modelMap.put("gateways", JSON.toJSONString(list));
        modelMap.put("customername", customerInfo.getCustomername() == null ? "" : customerInfo.getCustomername());
        modelMap.put("companycode", customerInfo.getCompanycode() == null ? "" : customerInfo.getCompanycode());
        modelMap.put("allnight", allnight);
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        modelMap.put("endDate", endDate);
        return new ModelAndView("warning/allnightList", modelMap);
    }

    @RequestMapping(value = "/gatewayNoData")
    public ModelAndView gatewayNoData(String gatewayLogo, ModelMap modelMap) {
        JdbcUtils.changeDataSource(Constants.COLLECT_DB);
        SqlBuffer sqlBuffer = new SqlBuffer("SELECT " +
                "  gateway_logo, " +
                "  sensor_name, " +
                "  min(channel_name) channel_name, " +
                "  max(data_time)    data_time " +
                "FROM data " +
                "WHERE data_time < '" + DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -1) + "' " + (StringUtils.isNotNullOrEmpty(gatewayLogo) ? "AND gateway_logo = '" + gatewayLogo + "'" : "") +
                "GROUP BY gateway_logo, sensor_name " +
                "ORDER BY gateway_logo, sensor_name, channel_name, data_time ", false);
        List<RealData> realDataList = JdbcUtils.query(RealData.class, sqlBuffer.getSql());
        JdbcUtils.changeDataSource(Constants.DEFAULT_DB);
        List<RealData> result = new ArrayList<RealData>();
        for (int i = 0; i < realDataList.size(); i++) {
            RealData realData = realDataList.get(i);
            AreaPoint areaPoint = novaAreapointServices.getByDetail(realData.getGatewayLogo(), realData.getSensorName(), realData.getChannelName());
            if (areaPoint != null) {
                realData.setCompanyName(areaPoint.getCompanyname());
                realData.setAreaId(areaPoint.getAreaid());
                result.add(realData);
            }
        }
        modelMap.put("gatewayLogo", gatewayLogo);
        modelMap.put("result", result);
        return new ModelAndView("warning/gatewayNoData", modelMap);
    }

    @RequestMapping(value = "/dataMissedInit")
    public ModelAndView dataMissedInit(HttpServletRequest request, HttpSession session,
                                       Integer pageIndex, ModelMap modelMap, String gatewayId,
                                       CustomerInfo customerInfo, String endDate) {

        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        if (StringUtils.isNullOrEmpty(endDate)) {
            endDate = DateUtils.getDate2SStr(DateUtils.getYesterdayDate());
        }
        List<String> customerGateway = new ArrayList<String>();
        if (StringUtils.isNullOrEmpty(customerInfo.getCustomername()) && !IntegerUtils.isEmptyAndNull(customerInfo.getCompanycode())) {
            Login login = (Login) session.getAttribute("sessionLogin");
            List<CustomerInfo> customerInfos = null;
            if (login.getRoletype() == ModelConstant.ROLE_GROUP) {
                customerInfos = novaCustomerInfoService.findByGroupcode(login.getGroupcode());
            } else {
                customerInfos = novaCustomerInfoService
                        .findCustomerInfoListByRole(login.getUsername(), null, "", "");
            }
            customerGateway = novaAreapointServices.getCustomerGateway(customerInfos);
        } else {
            List<CustomerInfo> customerInfoList = novaCustomerInfoService.findByCodeNameLike(customerInfo);
            List<String> gateway = novaAreapointServices.getCustomerGateway(customerInfoList);
            customerGateway.addAll(gateway);
        }
        List<Map<String, Object>> list = novaGatherService.getDataMissed(page, endDate, ModelConstant.GATHER_ERRORSTATE, customerGateway, ModelConstant.WARNING_TIME_MISSED);
        modelMap.put("customername", customerInfo.getCustomername() == null ? "" : customerInfo.getCustomername());
        modelMap.put("companycode", customerInfo.getCompanycode() == null ? "" : customerInfo.getCompanycode());
        modelMap.put("datamissed", list);
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        modelMap.put("endDate", endDate);
        return new ModelAndView("warning/datamissedList", modelMap);
    }

}
